package test.lemonyfei.http.utils;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import java.io.*;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by sh00556 on 2017/4/25.
 */
public class HttpHandle implements HttpHandler {
    private HttpContext httpContext;

    public HttpContext gethttpContext() {
        return this.httpContext;
    }

    public void sethttpContext(HttpContext httpContext) {
        this.httpContext = httpContext;
    }

    public HttpHandle(HttpContext httpContext) {
        sethttpContext(httpContext);
    }

    public void handle(HttpExchange exchange) throws IOException {
        this.httpContext.getAreaOfConsole().append(exchange.getRequestMethod() + " " + URLDecoder.decode(exchange.getRequestURI().toString(), "UTF-8") + "\n");
        Headers requestHeaders = exchange.getRequestHeaders();
        Set<String> keySet = requestHeaders.keySet();
        Iterator<String> iterOfRequestHeaders = keySet.iterator();
        while (iterOfRequestHeaders.hasNext()) {
            String key = iterOfRequestHeaders.next();
            List<String> values = requestHeaders.get(key);
            String s = key + " = " + values.toString() + "\n";
            this.httpContext.getAreaOfConsole().append(s);
        }
        String requestMethod = exchange.getRequestMethod();
        if (requestMethod.equals("POST")) {
            this.httpContext.setActualRequest(fromStream(exchange.getRequestBody()).trim());
            this.httpContext.getAreaOfConsole().append("\n" + this.httpContext.getActualRequest());
        }
        this.httpContext.getAreaOfConsole().append("\n\n");
        Headers responseHeaders = exchange.getResponseHeaders();
        if ((this.httpContext.getHeaders() != null) && (!this.httpContext.getHeaders().isEmpty())) {
            Iterator<Map.Entry<String, String>> iter = this.httpContext.getHeaders().entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<String, String> entry = (Map.Entry) iter.next();
                responseHeaders.set(entry.getKey(), entry.getValue());
            }
        }
        exchange.sendResponseHeaders(this.httpContext.getResultCode(), 0L);
        OutputStream responseBody = exchange.getResponseBody();
        String responseString = this.httpContext.getResponseBody();
        responseBody.write(responseString.getBytes());
        responseBody.close();
    }

    private String fromStream(InputStream in) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder out = new StringBuilder();
        String newLine = System.getProperty("line.separator");
        String line = null;
        while ((line = reader.readLine()) != null) {
            out.append(line);
            out.append(newLine);
        }
        return out.toString();
    }
}
